package utils

import (
	"bytes"
	"crypto/md5"
	"crypto/rand"
	"encoding/json"
	"fmt"
	"math/big"
	"os"
	"time"
	"reflect"
	"strconv"
	"strings"
)

//判断数组中是否存在某个值
func In_array( item string,items []string) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

//获取当前时间戳 -- 十位数
func Time() int64{
	return time.Now().Unix()
}
//获取当前单位纳秒时间戳
func TimeNano() int64{
	return  time.Now().UnixNano()
}

//返回当前时间 true:dateTime  false:date
func Date(status ...bool)  string{
	if len(status) > 0 && status[0] == true{
		return time.Now().Format("2006-01-02")
	}else{
		return time.Now().Format("2006-01-02 15:04:05")
	}
}

//时间 to 时间戳
func Strtotime(date string , status bool) interface{} {
	loc, _ := time.LoadLocation("Asia/Shanghai")        //设置时区
	if status == true{
		tt, _ := time.ParseInLocation("2006-01-02 15:04:05", date , loc)
		return tt.Unix()
	}else{
		tt, _ := time.ParseInLocation("2006-01-02", date , loc)
		return tt.Unix()
	}
}

//获取变量类型
func Typeof(v interface{}) string {
	return reflect.TypeOf(v).String()
}

//字符串转int
func Int(str string) int {
	int,err:=strconv.Atoi(str)
	if err != nil{
		int=0
	}
	return int;
}

//字符串转int64
func Int64(str string) int64 {
	int,err:=strconv.ParseInt(str,10,64)
	if err != nil{
		int=0
	}
	return int;
}

//int 转 字符串
func String(int int) string {
	str:=strconv.Itoa(int)
	return str;
}

//int 转 字符串
func String64(int int64) string {
	str:=strconv.FormatInt(int,10)
	return str;
}

//字符串转md5
func Md5(str string) string{
	data := []byte(str)
	has := md5.Sum(data)
	md5str1 := fmt.Sprintf("%x", has)
	return md5str1
}

//判断文件是否存在
func CheckFileIsExist(filename string) bool {
	var exist = true
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		exist = false
	}
	return exist
}

//结构体转map
func StructToMap(obj interface{}) map[string]string {
	obj_v := reflect.ValueOf(obj)
	v := obj_v.Elem()
	typeOfType := v.Type()
	var data = make(map[string]string)
	for i := 0; i < v.NumField(); i++ {
		field := v.Field(i)
		data[typeOfType.Field(i).Name] = field.Interface().(string)
	}
	return data
}

//随机指定长度的字符串
func CreateRandomString(len int) string  {
	var container string
	var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	b := bytes.NewBufferString(str)
	length := b.Len()
	bigInt := big.NewInt(int64(length))
	for i := 0;i < len ;i++  {
		randomInt,_ := rand.Int(rand.Reader,bigInt)
		container += string(str[randomInt.Int64()])
	}
	return container
}

//JSON压缩
func Json_encode(value interface{}) string{
	var key string
	newValue, _ := json.Marshal(value)
	key = string(newValue)
	return key
}

//截取两个字符串之间的字符
func GetBetweenStrs(str, start, end string) string {
	n := strings.Index(str, start)
	if n == -1 {
		n = 0
	}
	str = string([]byte(str)[n:])
	m := strings.Index(str, end)
	if m == -1 || end == "" {
		m = len(str)
	}
	str = string([]byte(str)[:m])
	return str
}

//字符串截取
func Substr(str string, start, length int) string {
	rs := []rune(str)
	rl := len(rs)
	end := 0
	if start < 0 {
		start = rl - 1 + start
	}
	end = start + length
	if start > end {
		start, end = end, start
	}
	if start < 0 {
		start = 0
	}
	if start > rl {
		start = rl
	}
	if end < 0 {
		end = 0
	}
	if end > rl {
		end = rl
	}
	return string(rs[start:end])
}